/**
 *省市区联动
 *@author Jacob
 */
function Region(args)
{
    var defaultProvince = args._defaultValue.province;
    var defaultCity = args._defaultValue.city;
    var defaultCountry = args._defaultValue.country;

    var provinceElement = args._elements.province;
    var cityElement = args._elements.city;
    var countryElement = args._elements.country;

    var defaultPid = args._pid;

    var provinceid = cityid = districtid = 0;
    if(args._ids){
         provinceid = args._ids.provinceid ? args._ids.provinceid : 0;
         cityid = args._ids.cityid ? args._ids.cityid : 0;
         districtid = args._ids.districtid ? args._ids.districtid : 0;
    }
    var requestURL = "http://www.wjyh.com/index.php?app=store/SMlselection&type=region";
    if(args.reqUrl){

        requestURL=args.reqUrl;
    }
    var init = function(){
        provinceElement.append(createHtml(provinceid,defaultProvince,true));
        cityElement.append(createHtml(cityid,defaultCity,true));
        countryElement.append(createHtml(districtid,defaultCountry,true));

        $.ajax({
            url:requestURL,
            data:{'pid':defaultPid},
            dataType:"json",
            success:function(response){
//                var data = eval('('+response+')');
                var regionInfo  = response.retval;
                if(response.done)
                {
                    if( regionInfo.length > 0)
                    {
                        for (i = 0; i < regionInfo.length; i++)
                        {
                            provinceElement.append(createHtml(regionInfo[i].region_id,regionInfo[i].region_name,false));
                        }
                    }
                }
            }
        });
    };

    this.changeEvent = function(obj)
    {
        var type = judgeSelectEle(obj);

        if(!type) return false;

        var pid = "";
        if(type == 1)//表示是省份选择框
        {
            if(parseInt(provinceElement.val()) == 0)
            {
                if(cityElement.length >= 1) cityElement.empty();
                if(countryElement.length >= 1) countryElement.empty();
                cityElement.append(createHtml("0",defaultCity,true));
                countryElement.append(createHtml("0",defaultCountry,true));
                return false;
            }

            pid = parseInt(provinceElement.val());

        }
        else if(type == 2)//表示是市级选择框
        {

            if(parseInt(provinceElement.val()) == 0)
            {
                return false;
            }

            if(parseInt(cityElement.val()) == 0)
            {
                if(countryElement.length >= 1) countryElement.empty();
                countryElement.append(createHtml("0",defaultCountry,true));
                return false;
            }

            pid = parseInt(cityElement.val());

        }

        $.ajax({
            url:requestURL,
            data:{'pid':pid},
            success:function(response){
                var data = eval('('+response+')');
                var regionInfo  = data.retval;

                if(data.done)
                {
                    if( regionInfo.length > 0)
                    {
                        var nextSelect = countryElement;
                        if(type == 1)
                        {
                            if(cityElement.length >= 1) cityElement.empty();
                            cityElement.append(createHtml("0",defaultCity,true));
                            nextSelect = cityElement;
                        }
                        if(countryElement.length >= 1) countryElement.empty();
                        countryElement.append(createHtml("0",defaultCountry,true));

                        for (i = 0; i < regionInfo.length; i++)
                        {
                            nextSelect.append(createHtml(regionInfo[i].region_id,regionInfo[i].region_name,false));
                        }
                    }
                }
            }
        })
    };

    /*判断当前是哪个下拉框*/
    var judgeSelectEle = function(obj)
    {
        var next_s = $(obj).nextAll("select");
        var cnt = next_s.length;

        if(cnt == 2)
        {
            return 1;
        }
        else if(cnt == 1)
        {
            return 2;
        }
        else
        {
            return false;
        }
    };

    var createHtml = function(optionValue,optionText,condition)
    {
        var selectedStr = condition?'selected="selected"':'';
        return '<option value="'+optionValue+'" '+selectedStr+'>' + optionText + '</option>';
    };

    this.upSelect = function(checkVObj)
    {
        $.each(args._elements,function(ele,item){
            $(item).empty();
            $(item).append(createHtml("0",args._defaultValue[ele],false));
            var valObj  = checkVObj[ele];
            var pid = valObj.pid;
            var region_id = valObj.region_id;
            $.ajax({
                type:'get',
                url:requestURL,
                data:{'pid':pid},
                success:function(response){
                    var data = eval('('+response+')');
                    var regionInfo  = data.retval;
                    if(data.done)
                    {
                        if( regionInfo.length > 0)
                        {
                            for (i = 0; i < regionInfo.length; i++)
                            {
                                var flag = (regionInfo[i].region_id == region_id)?true:false;
                                $(item).append(createHtml(regionInfo[i].region_id,regionInfo[i].region_name,flag));
                            }
                        }
                    }
                }
            });
        });
    };
    init();
}